package Dao.Impl;

import Dao.CommentDao;
import Dto.CommentDto;
import entity.Comment;
import utils.DBUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author Su
 * @Date 2025/5/8 9:52
 * @Version 1.0 （版本号）
 */
public class CommentDaoImpl implements CommentDao {

    private Connection connection;

    public CommentDaoImpl(Connection connection) {
        this.connection = connection;
    }

    @Override
    public boolean addComment(Comment comment) throws SQLException{
        String sql = "INSERT INTO article_comments (article_id, user_id, parent_id, root_id, content) VALUES (?, ?, ?, ?, ?)";

        try (PreparedStatement pstmt = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {

            pstmt.setInt(1, comment.getArticleId());
            pstmt.setInt(2, comment.getUserId());
            if (comment.getParentId() != null) {
                pstmt.setInt(3, comment.getParentId());
                pstmt.setInt(4, comment.getRootId());
            } else {
                pstmt.setNull(3, Types.INTEGER);//SQL中的整数类型（INTEGER）
                pstmt.setNull(4, Types.INTEGER);
            }
            pstmt.setString(5, comment.getContent());
            ;

            int row = pstmt.executeUpdate();
            if(row > 0){
                return true;
            }
        }
        return false;
    }

    @Override
    public CommentDto getCommentById(Integer parentId) throws SQLException {
        String sql = "SELECT * FROM article_comments WHERE id = ?";
        try (PreparedStatement pstmt = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {

            pstmt.setInt(1, parentId);

            try (ResultSet rs = pstmt.executeQuery()) {
                if (rs.next()) {
                    CommentDto commentDto = new CommentDto();
                    commentDto.setRootId(rs.getInt("root_id"));
                    return commentDto;
                }
            }
        }
        return null;
    }

    @Override
    public List<Comment> getComments(CommentDto commentReq)  throws SQLException {
        String sql;
        PreparedStatement pstmt;
        System.out.println("进入查询评论");
        System.out.println(commentReq);
//        if (commentReq.getRootId() != null) {
//            sql = "SELECT * FROM article_comments WHERE article_id = ? AND root_id = ? ORDER BY created_at DESC";
//            pstmt = connection.prepareStatement(sql);
//            pstmt.setInt(1, commentReq.getArticleId());
//            pstmt.setInt(2, commentReq.getRootId());
//        } else {
        sql = "SELECT * FROM article_comments WHERE article_id = ? ORDER BY created_at DESC";
        pstmt = connection.prepareStatement(sql);
        pstmt.setInt(1, commentReq.getArticleId());
//        }
        System.out.println("sql拼接完成");
        try (ResultSet rs = pstmt.executeQuery()) {
            List<Comment> comments = new ArrayList<>();
            while (rs.next()) {
                Comment comment = new Comment();
                comment.setContent(rs.getString("content"));
                comment.setUserId(rs.getInt("user_id"));
                comment.setParentId(rs.getInt("parent_id"));
                comment.setRootId(rs.getInt("root_id"));
                comment.setCreated_at(rs.getTimestamp("created_at"));
                comment.setId(rs.getInt("id"));
                comments.add(comment);
            }
            System.out.println("查询评论完成");
            return comments;
        }
    }
}
